@page "/fss/client/list"
@page "/fss/client/list/{pageIndex:int}"
@using FOPS.Abstract.Fss.Server
@using FOPS.Abstract.Fss.Entity
@using FS.Extends
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject Blazored.LocalStorage.ILocalStorageService storage;

<div class="layui-card">
    <div class="layui-card-header" id="anchor-play-header">
        客户端列表
    </div>
    <div class="layui-card-body">
        <div class="layui-form layui-border-box layui-table-view" lay-filter="LAY-table-1" style=" ">
            <div class="layui-table-box">
                <div class="layui-table-header">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <thead>
                        <tr>
                            <th data-field="ID">
                                <div class="layui-table-cell laytable-cell-14-0-0">
                                    <span>客户端Id</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-1">
                                    <span>客户端IP</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>客户端名称</span>
                                </div>
                            </th>
                            <th data-field="Games">
                                <div class="layui-table-cell laytable-cell-14-0-3">
                                    <span>使用时间</span>
                                </div>
                            </th>
                            <th data-field="Games">
                                <div class="layui-table-cell laytable-cell-14-0-4">
                                    <span>注册的Job</span>
                                </div>
                            </th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div class="layui-table-body layui-table-main">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <tbody>
                        @for (var index = 0; index < _lst.Count; index++)
                        {
                            var client = _lst[index];
                            <tr data-index="@index">
                                <td data-field="Type">
                                    <div class="layui-table-cell laytable-cell-14-0-0">@client.Id</div>
                                </td>
                                <td data-field="Type">
                                    <div class="layui-table-cell laytable-cell-14-0-1">@client.ClientIp</div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">@client.ClientName</div>
                                </td>
                                <td data-field="Items">
                                    <div class="layui-table-cell laytable-cell-14-0-3">@client.ActivateAt.ToString("yyyy-MM-dd HH:mm:ss")</div>
                                </td>
                                <td data-field="Games">
                                    <div class="layui-table-cell laytable-cell-14-0-4">@string.Join(",",client.Jobs)</div>
                                </td>
                            </tr>
                        }
                        </tbody>
                    </table>
                </div>
            </div>
            <PageSplit PageIndex="@PageIndex" PageSize="@_pageSize" TotalCount="@_totalCount" PageUrl="/fss/client/list/{index}"></PageSplit>
        </div>
    </div>
</div>
<style>
    .laytable-cell-14-0-0{ width: 200px; }
    .laytable-cell-14-0-1{ width: 110px; }
    .laytable-cell-14-0-2{ width: 300px; }
    .laytable-cell-14-0-3{ width: 150px; }
    .laytable-cell-14-0-4{ width: 600px; }
</style>

@code{

    private int _lastIndex;
    [Parameter] public int PageIndex { get; set; }
    private int _totalCount;
    private int _pageSize = 9999;

    private List<ClientVO> _lst = new();
    private bool _isFirst;

    protected override async Task OnParametersSetAsync()
    {
        if (_isFirst && _lastIndex == PageIndex) return;
        _isFirst = true;

        if (PageIndex < 1) PageIndex = 1;
        _lastIndex = PageIndex;

        _lst = await _iocManager.Resolve<IFssApi>().GetClientListAsync(storage) ?? new();
        _lst = _lst.OrderByDescending(o => o.ActivateAt).ToList();
        _totalCount = _lst.Count;
    }
}